package zqm.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import zqm.common.Result;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import zqm.service.ILunService;
import zqm.entity.Lun;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;


@RestController
@RequestMapping("/lun")
public class LunController {

    @Resource
    private ILunService lunService;

    // 新增或者更新
    @PostMapping
    public Result save(@RequestBody Lun lun) {
        lunService.saveOrUpdate(lun);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        lunService.removeById(id);
        return Result.success();
    }

    @PostMapping("/del/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        lunService.removeByIds(ids);
        return Result.success();
    }

    @GetMapping
    public Result findAll() {
        return Result.success(lunService.list());
    }

    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(lunService.getById(id));
    }

    @GetMapping("/page")
    public Result findPage(@RequestParam(defaultValue = "") String articleName,
                           @RequestParam Integer pageNum,
                                @RequestParam Integer pageSize) {
        System.out.println("articleName=" + articleName); // 日志输出
        QueryWrapper<Lun> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        if (articleName != null && !articleName.isEmpty()) {
            queryWrapper.like("article_name", articleName);
        }
        return Result.success(lunService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }

}

